<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="onshowDialog">
    <el-form :model="form" ref="form" :rules="rules" label-width="120px">
      <el-form-item prop="username" label="姓名">
        <el-input
          style="width: 70%"
          v-model="form.username"
          placeholder="请输入名字"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input
          style="width: 70%"
          v-model="form.mobile"
          placeholder="请输入手机号"
          maxlength="11"
        ></el-input>
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          type="date"
          placeholder="请选择日期"
          v-model="form.timeOfEntry"
        ></el-date-picker>
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :label="item.value"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input
          style="width: 70%"
          v-model="form.workNumber"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input
          v-model="form.departmentName"
          style="width: 70%"
          placeholder="请选择部门"
          @blur="isBlur"
          @focus="getDepartments"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          default-expand-all
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          type="date"
          placeholder="请选择日期"
          v-model="form.correctionTime"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onshowDialog">取 消</el-button>
      <el-button type="primary" @click="okadd">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addEmployee } from "@/api/employees.js";
import { getDepartment } from "@/api/department.js";
import { tranListToTreeData } from "@/utils";
// 导入枚举库
import enumObj from "@/api/constant/employees";
export default {
  data() {
    return {
      hireType: enumObj.hireType,
      // 表单数据
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      rules: {
        username: [
          { required: true, message: "用户性名不能为空", trigger: "blur" },
          { min: 1, max: 4, message: "用户姓名为1-4位" },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [
          { required: true, message: "入职时间不能为空", trigger: "change" },
        ],
      },
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    // 取消与关闭窗口
    onshowDialog() {
      this.form = {};
      this.$refs.form.resetFields();
      this.$emit("update:showDialog", false);
      this.showTree = false;
    },
    // 获取部门并转为树状结构
    async getDepartments() {
      this.showTree = true;
      this.loading = true;
      let { depts } = await getDepartment();
      this.treeData = tranListToTreeData(depts, "");
      this.loading = false;
    },
    selectNode(node) {
      this.form.departmentName = node.name;
      this.showTree = false;
    },
    isBlur(e) {
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.showTree = false;
      }
    },
    // 新增员工
    async okadd() {
      try {
        this.$refs.form.validate();
        await addEmployee(this.form);
        this.$parent.page.total += 1;
        this.$parent.page.page = Math.ceil(
          this.$parent.page.total / this.$parent.page.size
        );
        await this.$parent.getEmployeeList();
        this.form = {
          username: "",
          mobile: "",
          formOfEmployment: "",
          workNumber: "",
          departmentName: "",
          timeOfEntry: "",
          correctionTime: "",
        };
        this.$message.success("新增成功");
        this.$emit("update:showDialog", false);
      } catch (error) {
        console.log(error);
      }
    },
  },
  computed: {},
};
</script>

<style>
</style>